/* Printer status information. */
/* Handles internal variables */
/* Created 21.03.2005 T. Milius from split of other library
   Changed 12.09.2005 T. Milius Addition of variables INTERFACE and PROTOCOL
   Changed 15.10.2005 T. Milius Addition of variable CLEANING_LEVEL */
/* (c) Copyright 2005 by Thomas Milius Stade, Germany
   Source must not be altered without agreement of the owner.
   The owner of the source is allowed to use this code inside programs without
   publishing the code of this programs. These programs may be commercial.
   Other developers can use this source freely inside own software if the source
   code of this programs is made public to same conditions like valid to this code
   and no commercial profit is taken from the programs based on this code

   Code or parts of it are not allowed to be used within GPL code or
   similar licenses which are "infecting" other code and trying to "supersede"
   other licenses. */
/* ANSI C */

#ifndef variables_h
#define variables_h

/* !!!!!!!!!! libraries !!!!!!!!!! */
/* ---------- ANSI-C ---------- */
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* !!!!!!!!!!! definitions !!!!!!!!!! */
/* see colour definitons in documentation */
#define MIN_COLOUR   0x00
#define MAX_COLOUR   0x13
/* see cartridge definitons in documentation */
#define MIN_CARTRIDGE 0x30
#define MAX_CARTRIDGE 0x32
#define MAX_USER_VARIABLES        4
#define MAX_CARTRIDGE_TYPE_LENGTH 20
#define MAX_REMARK_LENGTH         40
#define MAX_RESPONSE_LENGTH       256
#define MAX_BUTTONS               3
#define MAX_BUTTON_LABEL_LENGTH   20
#define MAX_MESSAGE_LENGTH        50
#define MAX_STRING_LENGTH         256

/* Variables */
#define VARIABLE_TAG_COLOUR           "COL_"
#define VARIABLE_TAG_CARTRIDGE_MANFAC "CTRMAN"
#define VARIABLE_TAG_CARTRIDGE_TYPE   "CTRTYP_"
#define VARIABLE_TAG_CARTRIDGE_REMARK "RMRK_"
#define VARIABLE_TAG_MESSAGE          "MESSAGE"
#define VARIABLE_TAG_DIALOGMESSAGE    "DIALOGMESSAGE"
#define VARIABLE_TAG_DIALOGBUTTON     "DIALOGBUTTON_"
#define VARIABLE_TAG_DIALOGRESULT     "DIALOGRESULT"
#define VARIABLE_TAG_RESPONSE         "RESPONSE"
#define VARIABLE_TAG_MANUFACTURER     "MANUFACTURER"
#define VARIABLE_TAG_PRINTER_TYPE     "PRINTERTYPE"
#define VARIABLE_TAG_ERROR_STATUS     "ERROR"
#define VARIABLE_TAG_INTERFACE        "INTERFACE"
#define VARIABLE_TAG_PROTOCOL         "PROTOCOL"
#define VARIABLE_TAG_CLEANING_LEVEL   "CLEANINGLEVEL"
#define VARIABLE_TAG_TEXT             "TEXT_"
#define VARIABLE_TAG_INTEGER          "INT_"

#define VARIABLE_TYPE_CHAR 0
#define VARIABLE_TYPE_INTEGER 1

/* !!!!!!!!!! data structures !!!!!!!!!! */
struct variable_struct {
int colour[MAX_COLOUR - MIN_COLOUR + 1];
/* Colours can also occur as Catridges */
char cartridge_manufacturer[MAX_MESSAGE_LENGTH];
char *cartridge_type[MAX_CARTRIDGE - MIN_COLOUR + 1];
char *remark[MAX_CARTRIDGE - MIN_COLOUR + 1];
char message[MAX_MESSAGE_LENGTH];
char dialog_message[MAX_MESSAGE_LENGTH];
char dialog_button[MAX_BUTTONS][MAX_BUTTON_LABEL_LENGTH];
int dialog_result;
char response[MAX_RESPONSE_LENGTH];
char manufacturer[MAX_MESSAGE_LENGTH];
char printer_type[MAX_MESSAGE_LENGTH];
int error_status;
char interface[MAX_STRING_LENGTH];
char protocol[MAX_STRING_LENGTH];
int cleaning_level;
/* Arbitrary usage */
char *text[MAX_USER_VARIABLES];
int integer[MAX_USER_VARIABLES];
};

/* !!!!!!!!!! support functions !!!!!!!!!! */

/* !!!!!!!!!! functions !!!!!!!!!! */
bool initialize_variables(struct variable_struct *general_variables)
{
int i;

if (!general_variables) return false;
strcpy(general_variables->cartridge_manufacturer, "");
for (i=MIN_COLOUR; i <= MAX_CARTRIDGE; i++) {
  general_variables->cartridge_type[i]=NULL;
  general_variables->remark[i]=NULL;
  }
strcpy(general_variables->message, "");
strcpy(general_variables->dialog_message, "");
for (i=0; i < MAX_BUTTONS; i++) {
  strcpy(general_variables->dialog_button[i], "");
  }
strcpy(general_variables->response, "");
strcpy(general_variables->manufacturer, "");
strcpy(general_variables->printer_type, "");
general_variables->error_status=0;
strcpy(general_variables->interface, "");
strcpy(general_variables->protocol, "");
general_variables->cleaning_level=0;
for (i=0; i < MAX_USER_VARIABLES; i++) {
  general_variables->text[i]=NULL;
  }
return true;
}

int drop_cartridge_variables(struct variable_struct *general_variables)
{
int i;

if (!general_variables) return false;
strcpy(general_variables->cartridge_manufacturer, "");
for (i=MIN_COLOUR; i <= MAX_CARTRIDGE; i++) {
  if (general_variables->cartridge_type[i]) {
    free(general_variables->cartridge_type[i]);
    general_variables->cartridge_type[i]=NULL;
    }
  if (general_variables->remark[i]) {
    free(general_variables->remark[i]);
    general_variables->remark[i]=NULL;
    }
  }
return true;
}

bool drop_variables(struct variable_struct *general_variables)
{
int i;

if (!general_variables) return false;
drop_cartridge_variables(general_variables);
for (i=0; i < MAX_USER_VARIABLES; i++) {
  if (general_variables->text[i]) {
    free(general_variables->text[i]);
    }
  }
return initialize_variables(general_variables);
}

void *get_address_of_variable(struct variable_struct *general_variables,
                              char *name_of_variable,
                              int *type_of_variable,
                              int *size_of_variable)
{
int i;

if (!general_variables) return NULL;
if (!name_of_variable) return NULL;
if (strncmp(name_of_variable, VARIABLE_TAG_COLOUR, strlen(VARIABLE_TAG_COLOUR)) == 0) {
  sscanf(name_of_variable + strlen(VARIABLE_TAG_COLOUR), "%x", &i);
  if ((i < MIN_COLOUR) ||
      (i > MAX_COLOUR)) {
    return NULL;
    }
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_INTEGER;
    }
  if (size_of_variable) {
    *size_of_variable=sizeof(int);
    }
  return &general_variables->colour[i];
  }
else if (strncmp(name_of_variable, VARIABLE_TAG_CARTRIDGE_MANFAC, strlen(VARIABLE_TAG_CARTRIDGE_MANFAC)) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_MESSAGE_LENGTH;
    }
  return general_variables->cartridge_manufacturer;
  }
else if (strncmp(name_of_variable, VARIABLE_TAG_CARTRIDGE_TYPE, strlen(VARIABLE_TAG_CARTRIDGE_TYPE)) == 0) {
  sscanf(name_of_variable + strlen(VARIABLE_TAG_CARTRIDGE_TYPE), "%x", &i);
  if ((i < MIN_COLOUR) ||
      (i > MAX_CARTRIDGE)) {
    return NULL;
    }
  if (!general_variables->cartridge_type[i]) {
    if (!(general_variables->cartridge_type[i]=malloc(MAX_CARTRIDGE_TYPE_LENGTH))) {
      return NULL;
      }
    strcpy(general_variables->cartridge_type[i], "");
    }
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_CARTRIDGE_TYPE_LENGTH;
    }
  return general_variables->cartridge_type[i];
  }
else if (strncmp(name_of_variable, VARIABLE_TAG_CARTRIDGE_REMARK, strlen(VARIABLE_TAG_CARTRIDGE_REMARK)) == 0) {
  sscanf(name_of_variable + strlen(VARIABLE_TAG_CARTRIDGE_REMARK), "%x", &i);
  if ((i < MIN_COLOUR) ||
      (i > MAX_CARTRIDGE)) {
    return NULL;
    }
  if (!general_variables->remark[i]) {
    if (!(general_variables->remark[i]=malloc(MAX_REMARK_LENGTH))) {
      return NULL;
      }
    strcpy(general_variables->remark[i], "");
    }
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_REMARK_LENGTH;
    }
  return general_variables->remark[i];
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_MESSAGE) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_MESSAGE_LENGTH;
    }
  return general_variables->message;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_DIALOGMESSAGE) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_MESSAGE_LENGTH;
    }
  return general_variables->dialog_message;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_DIALOGBUTTON) == 0) {
  sscanf(name_of_variable + strlen(VARIABLE_TAG_DIALOGBUTTON), "%x", &i);
  if ((i < 0) ||
      (i >= MAX_BUTTONS)) {
    return NULL;
    }
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_BUTTON_LABEL_LENGTH;
    }
  return general_variables->dialog_button[i];
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_DIALOGRESULT) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_INTEGER;
    }
  if (size_of_variable) {
    *size_of_variable=sizeof(int);
    }
  return &general_variables->dialog_result;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_RESPONSE) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_RESPONSE_LENGTH;
    }
  return general_variables->response;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_MANUFACTURER) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_MESSAGE_LENGTH;
    }
  return general_variables->manufacturer;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_PRINTER_TYPE) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_MESSAGE_LENGTH;
    }
  return general_variables->printer_type;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_ERROR_STATUS) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_INTEGER;
    }
  if (size_of_variable) {
    *size_of_variable=sizeof(int);
    }
  return &general_variables->error_status;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_INTERFACE) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_STRING_LENGTH;
    }
  return general_variables->interface;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_PROTOCOL) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_STRING_LENGTH;
    }
  return general_variables->protocol;
  }
else if (strcmp(name_of_variable, VARIABLE_TAG_CLEANING_LEVEL) == 0) {
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_INTEGER;
    }
  if (size_of_variable) {
    *size_of_variable=sizeof(int);
    }
  return &general_variables->cleaning_level;
  }
else if (strncmp(name_of_variable, VARIABLE_TAG_TEXT, strlen(VARIABLE_TAG_TEXT)) == 0) {
  sscanf(name_of_variable + strlen(VARIABLE_TAG_TEXT), "%x", &i);
  if ((i < 0) ||
      (i >= MAX_USER_VARIABLES)) {
    return NULL;
    }
  if (!general_variables->text[i]) {
    if (!(general_variables->text[i]=malloc(MAX_RESPONSE_LENGTH))) {
      return NULL;
      }
    strcpy(general_variables->text[i], "");
    }
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_CHAR;
    }
  if (size_of_variable) {
    *size_of_variable=MAX_RESPONSE_LENGTH;
    }
  return general_variables->text[i];
  }
else if (strncmp(name_of_variable, VARIABLE_TAG_INTEGER, strlen(VARIABLE_TAG_INTEGER)) == 0) {
  sscanf(name_of_variable + strlen(VARIABLE_TAG_INTEGER), "%x", &i);
  if ((i < 0) ||
      (i >= MAX_USER_VARIABLES)) {
    return NULL;
    }
  if (type_of_variable) {
    *type_of_variable=VARIABLE_TYPE_INTEGER;
    }
  if (size_of_variable) {
    *size_of_variable=sizeof(int);
    }
  return &general_variables->integer[i];
  }
else {
  return NULL;
  }
}

#endif
